package com.woniu.ownermanage.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.LicensePlateOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.LicensePlateOCRResponse;
import com.woniu.common.ResponseData;
import com.woniu.ownermanage.entity.ParkingRecord;
import com.woniu.ownermanage.entity.ScmspOwnerCar;
import com.woniu.ownermanage.entity.ScmspOwnerCarOrder;
import com.woniu.ownermanage.service.ParkingRecordService;
import com.woniu.ownermanage.mapper.ParkingRecordMapper;
import com.woniu.ownermanage.service.ScmspOwnerCarOrderService;
import com.woniu.ownermanage.service.ScmspOwnerCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author MI
* @description 针对表【parking_record】的数据库操作Service实现
* @createDate 2025-01-10 18:21:47
*/
@Service
public class ParkingRecordServiceImpl extends ServiceImpl<ParkingRecordMapper, ParkingRecord>
    implements ParkingRecordService{

    @Value("${tencentcloudapi.secretId}")
    private String secretId;
    @Value("${tencentcloudapi.secretKey}")
    private String secretKey;
    @Value("${tencentcloudapi.endpoint}")
    private String endpoint;
    @Value("${tencentcloudapi.region}")
    private String region;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ScmspOwnerCarService scmspOwnerCarService;
    @Autowired
    private ScmspOwnerCarOrderService scmspOwnerCarOrderService;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;

    @Override
    public ResponseData<?> addCarParkingRecord(String image) {
        ParkingRecord parkingRecord = null;
        image = JSONUtil.parseObj(image).getStr("imageBase");
        String carId = getCarId(image);
        //先判断是否是业主的车，业主车不收费，临时停车收费
        ScmspOwnerCar car = scmspOwnerCarService.lambdaQuery()
                .eq(ScmspOwnerCar::getParkingNo, carId)
                .one();
        if (car!=null){
            return new ResponseData<>().ok("业主免费");
        }
        //判断是否有停车记录
        parkingRecord = lambdaQuery()
                .eq(ParkingRecord::getCarId, carId)
                .eq(ParkingRecord::getStatus, 0)
                .one();
        if (parkingRecord==null){
            Snowflake snowflake = new Snowflake(1, 1);
            long nextId = snowflake.nextId();
            //没有停车记录，添加停车记录
            ParkingRecord parkingRecord2 = new ParkingRecord();
            parkingRecord2.setOrderId(nextId);
            parkingRecord2.setCarId(carId);
            parkingRecord2.setStatus(0);
            parkingRecord2.setStartTime(DateUtil.date());
            save(parkingRecord2);
            //添加缴费记录
            ScmspOwnerCarOrder scmspOwnerCarOrder = new ScmspOwnerCarOrder();
            scmspOwnerCarOrder.setId(nextId);
            scmspOwnerCarOrder.setMoney(0);
            scmspOwnerCarOrder.setCarId(parkingRecord2.getCarId());
            scmspOwnerCarOrder.setStatus(0);
            scmspOwnerCarOrderService.save(scmspOwnerCarOrder);
            return new ResponseData<>().ok();
        }else {
            //有停车记录，更新结束时间
            parkingRecord.setEndTime(DateUtil.date());
            //parkingRecord.setStatus(1);
            updateById(parkingRecord);
            long parkingTime = getParkingTime(parkingRecord.getStartTime(), parkingRecord.getEndTime());
            return new ResponseData<>().ok(parkingTime);
        }
    }

    @Override
    public ResponseData<?> getAllRecord() {
        return new ResponseData<>().ok(parkingRecordMapper.getAllRecord());
    }


    private long getParkingTime(Date startTime, Date endTime) {
        long time = endTime.getTime() - startTime.getTime();
        return time;
    }

    private String getCarId(String image) {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient ocrClient = new OcrClient(cred, region, clientProfile);
            LicensePlateOCRRequest request = new LicensePlateOCRRequest();
            request.setImageBase64(image);
            LicensePlateOCRResponse response = ocrClient.LicensePlateOCR(request);
            return response.getNumber();
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
    }
}




